"use strict"

const os = require('os');
const fs = require('fs')
const path = require('path')
const webpack = require('webpack')
const ExtractTextPlugin = require("extract-text-webpack-plugin");

const version = process.env.VERSION || require('./package.json').version

console.log('---------------------------');
console.log(process.env.NODE_ENV);
console.log('---------------------------\n');

//get local ip address
let getIPAddress = () => {
    let interfaces = os.networkInterfaces();
    for (let devName in interfaces) {
        if( devName.toLowerCase().indexOf('vpn') < 0) {
            let iface = interfaces[devName];
            for (let alias of iface) {
                if (alias.family === 'IPv4' && alias.address !== '127.0.0.1' && !alias.internal) {
                    return alias.address;
                }
            }
        }
    }
}

module.exports = {
    context: __dirname,
    node: {
        __dirname: true
    },
    devtool: process.env.NODE_ENV === 'development' ? 'cheap-module-eval-source-map' : false,//cheap-module-eval-source-map
    // entry: ,
    entry: path.join(__dirname, 'app', 'main.js'),
    output: {
        path: path.join(__dirname, 'app'),
        filename: 'assets/scripts/build' + '.js',
        chunkFilename: '[id].chunk.js',
        publicPath: '/app/'
    },
    resolve: {
        alias: [{ 'vue': 'vue/dist/vue.min' }],
        extensions: ['', '.js', '.json', '.jsx', '.vue']
    },
    module: {
        loaders: [
            { test: /\.(png|jpg|woff|woff2|eot|ttf|svg)$/, include: path.resolve(__dirname, 'app'), loader: 'url-loader?limit=8192' },
            { test: /\.js$/, exclude: /node_modules|vue\/dist|vue-hot-reload-api|vue-loader/, loader: 'babel-loader' },
            { test: /\.vue$/, loader: 'vue-loader' },
            { test: /\.less$/, include: path.resolve(__dirname, 'app'), loader: "style-loader!css-loader!autoprefixer-loader!less-loader" },
        ]
    },
    devServer: {
        host: getIPAddress(),
        contentBase: "./",
        port: 8084,
        open: true,
        disableHostCheck: true,
        compress: true,
        pathinfo: true,
        watchOptions: {
            ignored: /node_modules/
        }
    },
    banner: `/**
 * app v${version}
 * (c) ${new Date().getFullYear()} 
 */`,
    plugins: [
        new webpack.optimize.UglifyJsPlugin({ compress: { warnings: false }, output: { comments: false } }),
        new webpack.DefinePlugin({ 'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV || 'development') })
    ]
}